package org.baixin.manager.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.baixin.core.Page;
import org.baixin.core.PropertyFilter;
import org.baixin.dao.base.HibernateDao;
import org.baixin.manager.BaseManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * 
 * 实现基本接口方法
 * 
 * @author slaton.wu@gmail.com
 * @version 1.0
 * @created 2011-6-19 下午02:12:02
 */
@Transactional
public abstract class BaseManagerImpl<T, PK extends Serializable, EntityDao extends HibernateDao<T, PK>>
		implements BaseManager<T, PK> {

	protected Logger logger = Logger.getLogger(getClass());

	public EntityDao entityDao;

	/**
	 * @param t
	 * @return
	 * @see org.baixin.manager.BaseManager#add(java.lang.Object)
	 */
	@Override
	public T add(T t) {
		entityDao.save(t);
		return t;
	}

	/**
	 * @param id
	 * @return
	 * @see org.baixin.manager.BaseManager#delete(java.io.Serializable)
	 */
	@Override
	public void delete(PK id) {
		entityDao.delete(id);
	}

	/**
	 * @param id
	 * @return
	 * @see org.baixin.manager.BaseManager#get(java.io.Serializable)
	 */
	@Override
	public T get(PK id) {

		return entityDao.get(id);
	}

	/**
	 * @param t
	 * @return
	 * @see org.baixin.manager.BaseManager#update(java.lang.Object)
	 */
	@Override
	public void update(T t) {

		entityDao.update(t);
	}

	/**
	 * @param page
	 * @return
	 * @see org.baixin.manager.BaseManager#getAll(org.baixin.core.Page)
	 */
	@Transactional(readOnly = true)
	@Override
	public Page<T> getAll(Page<T> page) {

		return entityDao.getAll(page);
	}

	public abstract void setEntityDao(EntityDao entityDao);

	/**
	 * @param propertyFilters
	 * @return
	 * @see org.baixin.manager.BaseManager#search(java.util.List)
	 */
	@Transactional(readOnly = true)
	@Override
	public Page<T> search(Page<T> page, List<PropertyFilter> filters) {

		return entityDao.findPage(page, filters);
	}

	/**
	 * @param filters
	 * @return
	 * @see org.baixin.manager.BaseManager#getList(java.util.List)
	 */
	@Transactional(readOnly = true)
	@Override
	public List<T> getList(List<PropertyFilter> filters) {
		
		return entityDao.find(filters);
	}
	
	@Transactional(readOnly = true)
	public List<T> getAll(){
		return entityDao.getAll();
	}
	
	public List<T> getAll(String propertyName,Object value){
		
		return entityDao.findBy(propertyName, value);
	}

}
